#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <curses.h>
#include "caml.h"
#include "structures.h"
#include "setter.h"
#include "debug.h"
#include "constantes.h"

value			set_screen_size(value			ba)
{
  int			*p;
  p_context		c;

  p = Data_bigarray_val(ba);
  c = p[IBA_CONTEXT];

/* #ifndef DEBUG */
  clear(); /* remove any ffmpeg / pulse text info / error msg */
/* #endif */
  getmaxyx(stdscr, p[IBA_SCREEN_LINE], p[IBA_SCREEN_COL]);

  c->line = p[IBA_SCREEN_LINE];
  c->col = p[IBA_SCREEN_COL];

  delwin(WMAIN);
  WMAIN = subwin(stdscr, c->line, c->col, 0, 0);

  p[IBA_SCREEN_LINE] = c->line;
  p[IBA_SCREEN_COL] = c->col;

  /* set_video_zone(ba); */

  /* grepme current error 110524 */
  set_subwindows(c, p[IBA_SCREEN_LINE], p[IBA_SCREEN_COL],
		 p[IBA_VIDEO_LINE], p[IBA_VIDEO_COL]);


  return (Val_unit);
}


/* called each resize querry */
value			set_video_zone(value			ba)
{
  int			*p;
  p_context		c;

  p = Data_bigarray_val(ba);
  c = p[IBA_CONTEXT];

  delwin(WVIDEO);

  WVIDEO = subwin(WMAIN, p[IBA_VID_NBCHAR_HEIGHT], p[IBA_VID_NBCHAR_WIDTH],
		  (p[IBA_SCREEN_LINE] - p[IBA_VID_NBCHAR_HEIGHT]) / 2,
		  (p[IBA_SCREEN_COL] - p[IBA_VID_NBCHAR_WIDTH]) / 2);

  getmaxyx(WVIDEO, p[IBA_VIDEO_LINE], p[IBA_VIDEO_COL]);

  /* redraw(c); */

  return (Val_unit);
}



void			init_subwindows_size(p_context		c)
{
  HEIGHT_EXPLORER = c->line;
  HEIGHT_PLAYLIST = c->line;
  WIDTH_INFOBAR = c->col;
  WIDTH_MENUBAR = c->col;

  /* grepme cstes in subwins def */
  WIDTH_PLAYLIST = c->col / 5;
  WIDTH_EXPLORER = c->col / 5;

  HEIGHT_INFOBAR = c->line / 8;
  HEIGHT_MENUBAR = c->line / 8;

  WIDTH_THUMB = WIDTH_PLAYLIST - 2 * IS_FRAME;
  HEIGHT_THUMB = HEIGHT_PLAYLIST / 6;
}


void			set_subwindows(p_context		c,
				       int			scr_line,
				       int			scr_col,
				       int			vid_line,
				       int			vid_col)
{
  HEIGHT_EXPLORER = scr_line;
  HEIGHT_PLAYLIST = scr_line;
  WIDTH_INFOBAR = scr_col;
  WIDTH_MENUBAR = scr_col;

  WIDTH_PLAYLIST = max2int(scr_col / 5, /* grepme cste */
			   (scr_col - vid_col) / 2);

  WIDTH_EXPLORER = max2int(scr_col / 5, /* grepme cste */
			   (scr_col - vid_col) / 2);

  WIDTH_THUMB = WIDTH_PLAYLIST - 2 * IS_FRAME;
  HEIGHT_THUMB = HEIGHT_PLAYLIST / 6; /* grepme cste */

  HEIGHT_INFOBAR = max2int(scr_line / 8,
			   (scr_line - vid_line) / 2);

  HEIGHT_MENUBAR = max2int(scr_line / 8,
			   (scr_line - vid_line) / 2);

  /* HEIGHT[ZONE_INFOBAR] *= HEIGHT_FONT + 2 * IS_FRAME; */
  /* HEIGHT[ZONE_MENUBAR] *= HEIGHT_FONT + 2 * IS_FRAME; */
}
